# Maintainer: Alexey Pavlov <alexpux@gmail.com>

# Before building package need CD into directory with drive letter, like
# cd /c/build/mingw-w64-ruby

# be sure to run the updpkgprovs and rebuild.  This is to get the propper version
# of the ruby gems that were bundled.

_realname=ruby
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.3
pkgrel=2
pkgdesc="An object-oriented language for quick and easy programming (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.ruby-lang.org/en"
license=("BSD, custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-libyaml"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-pdcurses"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-tk")
source=("https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/${_realname}-${pkgver}.tar.gz"
        0003-fix-check-types.patch
        0007-nm-use-full-options.patch
        0008-ucrt-32-win11-insns.patch)
## Populated by the updpkgprovs script
provides=(
    "${MINGW_PACKAGE_PREFIX}-ruby-debug=1.6.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-matrix=0.4.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-minitest=5.15.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-ftp=0.1.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-imap=0.2.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-pop=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-smtp=0.3.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-power_assert=2.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-prime=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-rake=13.0.6"
    "${MINGW_PACKAGE_PREFIX}-ruby-rbs=2.7.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-rexml=3.2.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-rss=0.2.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-test-unit=3.5.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-typeprof=0.21.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-abbrev=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-base64=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-benchmark=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-bigdecimal=3.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-bundler=2.3.26"
    "${MINGW_PACKAGE_PREFIX}-ruby-cgi=0.3.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-csv=3.2.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-date=3.2.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-delegate=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-did_you_mean=1.6.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-digest=3.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-drb=2.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-english=0.7.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-erb=2.2.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-error_highlight=0.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-etc=1.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fcntl=1.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-fiddle=1.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fileutils=1.6.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-find=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-forwardable=1.3.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-getoptlong=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-console=0.5.11"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-nonblock=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-wait=0.2.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-ipaddr=1.2.4"
    "${MINGW_PACKAGE_PREFIX}-ruby-irb=1.4.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-json=2.6.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-logger=1.5.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-mutex_m=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-http=0.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-protocol=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-nkf=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-observer=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-open-uri=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-open3=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-openssl=3.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-optparse=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-ostruct=0.5.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-pathname=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-pp=0.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-prettyprint=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-pstore=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-psych=4.0.4"
    "${MINGW_PACKAGE_PREFIX}-ruby-racc=1.6.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-rdoc=6.4.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-readline-ext=0.1.4"
    "${MINGW_PACKAGE_PREFIX}-ruby-readline=0.0.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-reline=0.3.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-resolv-replace=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-resolv=0.2.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-rinda=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-ruby2_keywords=0.0.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-securerandom=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-set=1.0.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-shellwords=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-singleton=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-stringio=3.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-strscan=3.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-tempfile=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-time=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-timeout=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-tmpdir=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-tsort=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-un=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-uri=0.11.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-weakref=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-win32ole=1.8.8"
    "${MINGW_PACKAGE_PREFIX}-ruby-yaml=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-zlib=2.1.1"
)
## End of pkgupdprovs modifications

sha256sums=('5ea498a35f4cd15875200a52dde42b6eb179e1264e17d78732c3a57cd1c6ab9e'
            'e5d665cabac8b7fbb0dda9a556c2350190801aa07b3cb90a0d50097ab99272a0'
            'b250c66bc8b372fb4c53902a6d56c01ad057416e3e368a5c5434d9a4ebdc3819'
            'c9f816baf5b2803e017924c7eeef6a478beb76247d6d4b39a2424c8d34687b2b')
noextract=(${_realname}-${pkgver}.tar.gz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg \
      0003-fix-check-types.patch \
      0007-nm-use-full-options.patch \
      0008-ucrt-32-win11-insns.patch

  autoreconf -fi
}

build() {

  local FFI_INC=$(pkg-config --cflags libffi)
  CPPFLAGS+=" -DFD_SETSIZE=2048 ${FFI_INC} -I${MINGW_PREFIX}/include/pdcurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CFLAGS+=" -Wno-unknown-attributes"
    CXXFLAGS+=" -Wno-unknown-attributes"
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-werror \
    $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && \
      echo "--with-setjmp-type=setjmp" \
           '--with-soname=$(RUBY_BASE_NAME)$(MAJOR)$(MINOR)0' || true )

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install-nodoc
  for script in {gem,}; do
    install ${srcdir}/ruby-${pkgver}/bin/${script} \
      ${pkgdir}${MINGW_PREFIX}/bin/
  done

  install -D -m644 ${srcdir}/${_realname}-${pkgver}/COPYING \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/LICENSE"
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/BSDL \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/BSDL"
}
